<template>
  <div class="login" id="login">
    <div class="login-wrap">
      <ul class="menu-tab">
        <li v-for="item in menuTab" :key="item.id" :class="{'current':item.current}" @click="toggleMenu(item)">{{item.text}}</li>
      </ul>
      <!-- form-start -->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="login-form" size="small">
        <label>邮箱</label>
        <el-form-item prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <label>密码</label>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off" maxlength="20"></el-input>
        </el-form-item>
        <label v-if="'register'===formType">确认密码</label>
        <el-form-item prop="repassword" v-if="'register'===formType">
          <el-input type="password" v-model="ruleForm.repassword" autocomplete="off" maxlength="20"></el-input>
        </el-form-item>
        <label>验证码</label>
        <el-form-item prop="code">
          <el-row :gutter="20">
            <el-col :span="14">
              <el-input v-model.number="ruleForm.code" maxlength="6"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button v-if="getMsgFlag" type="success" class="btn-block" @click="getMsg()">验证码</el-button>
              <el-button v-else type="success" class="btn-block" disabled>{{timeer}}</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" class="btn-block login-btn" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
      <!-- form-end -->
      <el-row class="copyright">
        <p>Copy right ©  rufeike 2019. All rights reserved</p>
      </el-row>
    </div>
  </div>
</template>

<script>
import {stripscript} from '@/utils/validate.js';
export default {
  name: "Login",
  data () {
    // 验证账号
    var username = (rule, value, callback) => {
      let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (value === '') {
        callback(new Error('请输入邮箱'));
      } else if (!reg.test(value)) {
        callback(new Error('邮箱格式有误'));
      } else {
        callback();
      }
    };
    // 密码验证
    var validatepassword = (rule, value, callback) => {
      this.ruleForm.password= stripscript(value);
      value = this.ruleForm.password;

      let reg = /^(?!\D+$)(?![^a-zA-Z]+$)\S{6,20}$/;
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (!reg.test(value)) {
        callback(new Error('密码位6至20位数字+字母'));
      } else {
        callback();
      }
    };

    // 确认密码验证
    var validaterepassword = (rule, value, callback) => {
      this.ruleForm.repassword= stripscript(value);
      value = this.ruleForm.repassword;

      if (value === '') {
        callback(new Error('请输入确认密码'));
      } else if (value!=this.ruleForm.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    };

    // 验证码验证码
    var checkCode = (rule, value, callback) => {
      let reg = /^[0-9]{6}$/;
      if (!value) {
        return callback(new Error('验证码不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (!reg.test(value)) {
            callback(new Error('验证码格式有误'));
          } else {
            callback();
          }
        }
      }, 600);
    };
    return {
      formType: 'login',
      getMsgFlag: true,
      timeer: 60,
      menuTab: [
        { text: "登录", current: true },
        { text: "注册", current: false }
      ],
      ruleForm: {
        username: '',
        password: '',
        repassword: '',
        code: '',
      },
      rules: {
        password: [
          { validator: validatepassword, trigger: 'blur' }
        ],
        repassword: [
          { validator: validaterepassword, trigger: 'blur' }
        ],
        username: [
          { validator: username, trigger: 'blur' }
        ],
        code: [
          { validator: checkCode, trigger: 'blur' }
        ]
      }
    }
  },
  created () { },
  mounted () { },
  methods: {
    toggleMenu (data) {
      this.menuTab.forEach(element => {
        element.current = false;
      });
      data.current = true;
      this.formType = data.text==='登录'?'login':'register';
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    getMsg(){
      this.getMsgFlag = false;
      while(this.timeer-->0){
        setTimeout(()=>{},1000);
      }
    }
  }
}
</script>

<style lang="scss" scorped>
#login {
  height: 100vh;
  background-color: #344a5f;
}
.login-wrap {
  width: 300px;
  margin:auto;
  padding: 100px 0;
}

.menu-tab {
  text-align: center;
  padding: 0;
  margin: 0;
  li {
    display: inline-block;
    width: 88px;
    line-height: 36px;
    font-size: 14px;
    color: #fff;
    border-radius: 2px;
    cursor: pointer;
  }
  .current {
    background-color: rgba(0, 0, 0, 0.1);
  }
}
.el-form-item--small .el-form-item__content,
.el-form-item--small .el-form-item__label {
  color: #fff;
}
.btn-block {
  display: block;
  width: 100%;
}
.login-btn {
  margin-top: 19px;
}
.login-form {
  margin-top: 30px;
  label {
    text-align: left;
    display: block;
    color: #fff;
    font-size: 14px;
    margin-bottom: 3px;
  }
}
.copyright{
  width:100%;
  color:#fff;
  text-align: center;
  font-size: 14px;
}
</style>
